
<?php
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'plant';
$return=array();
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='plant'){
	$post=get_postdata();
		if(!$_RQ['id']){
		exi('没有找到该土地，请重新选择！','error','jy_farmer/myhome/');
	}
	$plant_id = $_RQ['id'];
	$this->session('plant_id',$plant_id);
	$plant=pdo_get('jy_farmer_plant',array('uniacid'=>$_SESSION['uniacid'],'id'=>$plant_id));
	$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$plant['land_id']));
	$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']));
	$seed=pdo_getall('jy_farmer_myseed',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'sum >'=>0));
	if($seed){
		foreach($seed as $k=>&$item){
			$item['title']=pdo_getcolumn('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['seed_id']),'title');
			$item['image']=pdo_getcolumn('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['seed_id']),'image');
			$item['isarea']=pdo_getsum('jy_farmer_plant_log',array('uniacid'=>$_SESSION['uniacid'],'myseed_id'=>$item['id']),'plantarea');
			$item['isarea']=$item['isarea']?:0;
			$item['lastarea']=$item['sum']-$item['isarea'];
			if($item['lastarea']<=0){
				unset($seed[$k]);
			}
			$sum=$item['lastarea'];
			$area=array();
			for ($i=1; $i<=$sum; $i++) {
				array_push($area,$i);
			} 
			$item['area']=$area;
		}
	}
   $return['land']=$land;
   $return['seed']=$seed;
   $return['lclassify']=$lclassify;
   $return['post']=array('area'=>'');
   exi($return);
}
if($_RQ['op']=='notice'){
	$post=get_postdata();
	if(!$post['seed_id'] || !$post['area']){
		exi('请选择种子和种植面积！','error');
	}
	$plant_id =$this->session('plant_id');
	if($post){
			$insert=array(		
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'plant_id'=>$plant_id,
				'myseed_id'=>$post['seed_id'],
				'status'=>1,
				'plantarea'=>$post['area'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_farmer_plant_log',$insert);
			pdo_update('jy_farmer_plant',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$plant_id));
			exi('准备种植，将通知管理员种植','','jy_farmer/myplant/');
	}
}
?>
